package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

public class LeetCodeTest0617 {

    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(1);
        root1.left = new TreeNode(3);
        root1.right = new TreeNode(2);
        root1.left.left = new TreeNode(5);

        TreeNode root2 = new TreeNode(2);
        root2.left = new TreeNode(1);
        root2.right = new TreeNode(3);
        root2.left.right = new TreeNode(4);
        root2.right.right = new TreeNode(7);
        new LeetCodeTest0617().mergeTrees(root1, root2);
    }

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return null;
        }
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        TreeNode resultNode = new TreeNode(root1.val + root2.val);
        resultNode.left = mergeTreesLeft(root1.left, root2.left);
        resultNode.right = mergeTreesRight(root1.right, root2.right);
        return resultNode;
    }

    public TreeNode mergeTreesLeft(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return null;
        }
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        TreeNode resultNode = new TreeNode(root1.val + root2.val);
        resultNode.left = mergeTreesLeft(root1.left, root2.left);
        resultNode.right = mergeTreesRight(root1.right, root2.right);
        return resultNode;
    }

    public TreeNode mergeTreesRight(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return null;
        }
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        TreeNode resultNode = new TreeNode(root1.val + root2.val);
        resultNode.left = mergeTreesLeft(root1.left, root2.left);
        resultNode.right = mergeTreesRight(root1.right, root2.right);
        return resultNode;
    }

//    public void mergeTreesLeft(TreeNode resultNode, TreeNode root1, TreeNode root2) {
//        if (root1.left == null && root2.left == null) {
//            return;
//        }
//        if (root1.left == null) {
//            resultNode.left = root2.left;
//            return;
//        }
//        if (root2.left == null) {
//            resultNode.left = root1.left;
//            return;
//        }
//        resultNode.left = new TreeNode(root1.left.val + root2.left.val);
//        mergeTreesLeft(resultNode.left, root1.left, root2.left);
//        mergeTreesRight(resultNode.left, root1.right, root2.right);
//    }
//
//    public void mergeTreesRight(TreeNode resultNode, TreeNode root1, TreeNode root2) {
//        if (root1.right == null && root2.right == null) {
//            return;
//        }
//        if (root1.right == null) {
//            resultNode.right = root2.right;
//            return;
//        }
//        if (root2.right == null) {
//            resultNode.right = root1.right;
//            return;
//        }
//        resultNode.right = new TreeNode(root1.right.val + root2.right.val);
//        mergeTreesLeft(resultNode.right, root1.left, root2.left);
//        mergeTreesRight(resultNode.right, root1.right, root2.right);
//    }
}
